package com.hu.zhcc.shiro.controller.xsf;

import com.alibaba.fastjson.JSONObject;
import com.hu.zhcc.common.entity.Page;
import com.hu.zhcc.shiro.entity.lgx.*;
import com.hu.zhcc.shiro.entity.xsf.dto.TDiseaseLibraryDTO;
import com.hu.zhcc.shiro.entity.xsf.pojo.TDisease;
import com.hu.zhcc.shiro.entity.xsf.pojo.TDiseaseLibrary;
import com.hu.zhcc.shiro.entity.xsf.vo.TDiseaseLibraryVO;
import com.hu.zhcc.shiro.service.lgx.TMembermedicalhistoryService;
import com.hu.zhcc.shiro.service.lgx.TmemberService;
import com.hu.zhcc.shiro.service.xsf.TDiseaseLibraryService;
import com.hu.zhcc.shiro.service.xsf.TDiseaseService;
import com.hu.zhcc.shiro.service.xsf.TMealsService;
import org.apache.commons.lang3.StringUtils;
import org.hibernate.validator.internal.constraintvalidators.bv.past.PastValidatorForReadableInstant;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

import java.text.SimpleDateFormat;
import java.util.*;

@RestController
@RequestMapping("/tDisease")
public class TDiseaseController {

    @Autowired
    private TDiseaseLibraryService tDiseaseService;
    @Autowired
    private TDiseaseService diseaseService;
    @Autowired
    private TmemberService tmemberService;
    @Autowired
    private TMembermedicalhistoryService tMembermedicalhistoryService;

    @GetMapping("")
    public ResponseEntity<Page<TDiseaseLibraryVO>> getRoleList(@RequestParam(required = false) String name,
                                                               int offset, int limit) {
        Map<String, Object> paremeters = new HashMap<String, Object>();
        if (!StringUtils.isBlank(name)) {
            paremeters.put("name", name);
        }
        Page<TDiseaseLibraryDTO> result = this.tDiseaseService.queryAllTDisease(paremeters, offset, limit);
        List<TDiseaseLibraryVO> voList = new ArrayList<TDiseaseLibraryVO>();
        if( result.getRows()!=null ){

            for(TDiseaseLibraryDTO dto : result.getRows()) {
                TDiseaseLibraryVO vo = new TDiseaseLibraryVO();
                BeanUtils.copyProperties(dto, vo);
                voList.add(vo);
            }
//            System.out.println(voList);
            return ResponseEntity.ok(new Page<TDiseaseLibraryVO>(result.getTotal(), voList));
        }else {
            return null;
        }
    }
    @GetMapping("/queryDiseaseByMember/{id}")
    public List<TDisease> queryDiseaseByMember(@PathVariable("id") int id){
        System.out.println(diseaseService.queryDiseaseByMemberId(id));
        return diseaseService.queryDiseaseByMemberId(id);
    }

    @GetMapping("/queryAllTDisease1")
    public List<TDiseaseLibrary> queryAllTDisease1(){
        return tDiseaseService.queryAllTDisease1();
    }

    @GetMapping("/{id}")
    public ResponseEntity<TDiseaseLibraryVO> getById(@PathVariable("id") int id) {
        TDiseaseLibraryDTO dto = this.tDiseaseService.getById(id);
        if(dto == null) {
            return ResponseEntity.notFound().build();
        }

        TDiseaseLibraryVO vo = new TDiseaseLibraryVO();
        BeanUtils.copyProperties(dto, vo);
        return ResponseEntity.ok(vo);
    }

    @PutMapping("/{id}")
    public ResponseEntity<Integer> updateTEvaluate(@PathVariable("id") int id, @RequestBody TDiseaseLibrary vo) {
//        System.out.println(vo);
        int rows = this.tDiseaseService.updateTDiseaseLibrary(vo);
        return rows == 0 ? ResponseEntity.notFound().build() :
                ResponseEntity.status(HttpStatus.CREATED).body(rows);
    }
    @DeleteMapping("/{id}")
    public ResponseEntity<?> removeTEvaluate(@PathVariable("id") int id) {
        return this.tDiseaseService.removeTDiseaseLibrary(id) > 0 ?
                ResponseEntity.noContent().build() : ResponseEntity.notFound().build();
    }

    @PostMapping("/updateDiseaseById/{id}")
    @Transactional
    public int archiveadd(@PathVariable("id") int id,@RequestBody Map<String,String> map){


        int row = diseaseService.deleteByMemberId(id);
//        病历史
        String medicalHistory="";
//        家族史
        String familyHistory="";

        String dearDiseaseHas = map.get("dearDiseaseHas");
        Integer[] dearDisease = JSONObject.parseObject(dearDiseaseHas, Integer[].class);
        for(int i=0;i<dearDisease.length;i++){
            TDisease tDisease = new TDisease();
            tDisease.setDiseaseLibraryLib(dearDisease[i]);
            //父母
            tDisease.setWho(1);
            tDisease.setMemberId(id);
            diseaseService.insertSelective(tDisease);

            familyHistory+=tDiseaseService.getById(dearDisease[i]).getName();
            if(i<dearDisease.length-1){
                familyHistory+=",";
            }
        }

        String myDiseaseHas = map.get("myDiseaseHas");
        Integer[] myDisease = JSONObject.parseObject(myDiseaseHas, Integer[].class);
        for(int i=0;i<myDisease.length;i++){
            TDisease tDisease = new TDisease();
            tDisease.setDiseaseLibraryLib(myDisease[i]);
            //本人
            tDisease.setWho(0);
            tDisease.setMemberId(id);
            diseaseService.insertSelective(tDisease);

            medicalHistory+=tDiseaseService.getById(myDisease[i]).getName();
            if(i<myDisease.length-1){
                medicalHistory+=",";
            }
        }

        List<TMemberMedicalhistory> tMemberMedicalhistory = tMembermedicalhistoryService.selectByMember(id);
        tMemberMedicalhistory.get(0).setFamilyHistory(familyHistory);
        tMemberMedicalhistory.get(0).setMedicalHistory(medicalHistory);
        tMembermedicalhistoryService.updateByPrimaryKeySelective(tMemberMedicalhistory.get(0));


        return row;

    }
    @PostMapping("/addDiseaseById/{idcard}")
    @Transactional
    public int  addDiseaseById(@PathVariable("idcard") String idcard,@RequestBody Map<String,String> map){
        TMember tMember = tmemberService.selectByIdCard(idcard);

        int row = diseaseService.deleteByMemberId(tMember.getId());
//        病历史
        String medicalHistory="";
//        家族史
        String familyHistory="";

        String dearDiseaseHas = map.get("dearDiseaseHas");
        Integer[] dearDisease = JSONObject.parseObject(dearDiseaseHas, Integer[].class);
        for(int i=0;i<dearDisease.length;i++){
            TDisease tDisease = new TDisease();
            tDisease.setDiseaseLibraryLib(dearDisease[i]);
            //父母
            tDisease.setWho(1);
            tDisease.setMemberId(tMember.getId());
            diseaseService.insertSelective(tDisease);

            familyHistory+=tDiseaseService.getById(dearDisease[i]).getName();
            if(i<dearDisease.length-1){
                familyHistory+=",";
            }
        }

        String myDiseaseHas = map.get("myDiseaseHas");
        Integer[] myDisease = JSONObject.parseObject(myDiseaseHas, Integer[].class);
        for(int i=0;i<myDisease.length;i++){
            TDisease tDisease = new TDisease();
            tDisease.setDiseaseLibraryLib(myDisease[i]);
            //本人
            tDisease.setWho(0);
            tDisease.setMemberId(tMember.getId());
            diseaseService.insertSelective(tDisease);

            medicalHistory+=tDiseaseService.getById(myDisease[i]).getName();
            if(i<myDisease.length-1){
                medicalHistory+=",";
            }
        }

        return row;

    }
   /* @PostMapping("/insertDiseaseById/{id}")
    @Transactional
    public int insertDiseaseById(@PathVariable("id") int id,@RequestBody Map<String,String> map){


        int row = diseaseService.deleteByMemberId(id);
//        病历史
        String medicalHistory="";
//        家族史
        String familyHistory="";

        String dearDiseaseHas = map.get("dearDiseaseHas");
        Integer[] dearDisease = JSONObject.parseObject(dearDiseaseHas, Integer[].class);
        for(int i=0;i<dearDisease.length;i++){
            TDisease tDisease = new TDisease();
            tDisease.setDiseaseLibraryLib(dearDisease[i]);
            //父母
            tDisease.setWho(1);
            tDisease.setMemberId(id);
            diseaseService.insertSelective(tDisease);

            familyHistory+=tDiseaseService.getById(dearDisease[i]).getName();
            if(i<dearDisease.length-1){
                familyHistory+=",";
            }
        }

        String myDiseaseHas = map.get("myDiseaseHas");
        Integer[] myDisease = JSONObject.parseObject(myDiseaseHas, Integer[].class);
        for(int i=0;i<myDisease.length;i++){
            TDisease tDisease = new TDisease();
            tDisease.setDiseaseLibraryLib(myDisease[i]);
            //本人
            tDisease.setWho(0);
            tDisease.setMemberId(id);
            diseaseService.insertSelective(tDisease);

            medicalHistory+=tDiseaseService.getById(myDisease[i]).getName();
            if(i<myDisease.length-1){
                medicalHistory+=",";
            }
        }

        return row;

    }*/

    @PostMapping("")
        public ResponseEntity<TDiseaseLibraryVO> saveTEvaluate(@RequestBody TDiseaseLibraryVO vo) {
        TDiseaseLibraryDTO dto = new TDiseaseLibraryDTO();
        BeanUtils.copyProperties(vo, dto);
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
        dto.setCreateTime(simpleDateFormat.format(new Date()));
        this.tDiseaseService.saveTDiseaseLibrary(dto);
        vo.setId(dto.getId());
        return ResponseEntity.status(HttpStatus.CREATED).body(vo);
    }
}
